# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=pytest
pkgbase=mingw-w64-python3-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=5.2.2
pkgrel=1
pkgdesc='simple powerful testing with Python (mingw-w64)'
url='https://pytest.org/'
license=('MIT')
arch=('any')
install=${_realname}3-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-python3"
         "${MINGW_PACKAGE_PREFIX}-python3-atomicwrites>=1.0"
         "${MINGW_PACKAGE_PREFIX}-python3-attrs>=17.4.0"
         "${MINGW_PACKAGE_PREFIX}-python3-more-itertools>=4.0.0"
         "${MINGW_PACKAGE_PREFIX}-python3-pluggy>=0.7"
         "${MINGW_PACKAGE_PREFIX}-python3-py>=1.5.0"
         "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python3-six"
         "${MINGW_PACKAGE_PREFIX}-python3-colorama"
         "${MINGW_PACKAGE_PREFIX}-python3-wcwidth")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools-scm")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-hypothesis"
              "${MINGW_PACKAGE_PREFIX}-python3-mock"
              "${MINGW_PACKAGE_PREFIX}-python3-nose"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest-xdist"
              "${MINGW_PACKAGE_PREFIX}-python3-requests"
              "${MINGW_PACKAGE_PREFIX}-python3-tox"
              "${MINGW_PACKAGE_PREFIX}-python3-yaml")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/pytest-dev/pytest/archive/${pkgver}.tar.gz")
sha256sums=('90a29f993eaaf6d7d767bdf444e67e60f63434de5e7e9c986dd71c6c0f3a72e9')

prepare() {
  sed -i "s/'_pytest.vendored_packages'//" ${_realname}-${pkgver}/setup.py
  sed -i 's/assert numentries == 0/assert numentries == 26/' ${_realname}-${pkgver}/testing/python/collect.py
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}

  rm -rf python3-build-${CARCH}
  cp -r "${_realname}-${pkgver}" "python3-build-${CARCH}"
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {  
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py build
}

check() {
  cd "${srcdir}/python3-build-${CARCH}"
  # Python3: 1 failed, 2361 passed, 88 skipped, 12 xfailed, 1 warnings
  MSYSTEM= PYTHONPATH="$(pwd)/build/lib" \
    ${MINGW_PREFIX}/bin/python3 src/pytest.py || warning "Test failed"
}

package() {
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

  # You should OMIT this section if your package is not installing anything in the bin dir.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
